/// <reference path="Scripts/typings/easeljs/easeljs.d.ts" />

var stage: createjs.Stage;
var iteme: createjs.Bitmap;
var player = new Player("Assets/Characters/p1_stand.png");
var blocks = [];
var BLOCK_SIZE = 70;
var TILES_DIR = "Assets/Tiles";
var GROUND_TILE = TILES_DIR + "/grassMid.png";
var EXIT_TILE = TILES_DIR + "/signExit.png";
var HUD_HEART_FULL = "Assets/HUD/hud_heartFull.png";
var HUD_HEART_HALF = "Assets/HUD/hud_heartHalf.png";
var HUD_HEART_EMPTY = "Assets/HUD/hud_heartEmpty.png";

function drawHUD() {
    var life = new createjs.Bitmap(HUD_HEART_FULL);
    life.x = 10;
    life.y = 10;
    stage.addChild(life);
}

function generateLevel(level: number) {
    var lines = getMap(0);
    for (var i = 0; i < 5; i++) {
        for (var j = 0; j < 10; j++) {
            switch (lines[i][j]) {
                case "-":
                    break;
                case "=":
                    addBlock(GROUND_TILE, j, i);
                    break;
                case "x":
                    addBlock(EXIT_TILE, j, i);
                    break;
            }
        }
    }
}

function getMap(level: number) {
    var textFile = new XMLHttpRequest();
    var url = "Levels/" + level + ".txt";
    textFile.open("GET", url, false);
    textFile.send(null);
    return textFile.responseText.split("\n");
}

function addBlock(type: string, x: number, y: number) {
    var block = new Block(type);
    block.x = x * BLOCK_SIZE;
    block.y = y * BLOCK_SIZE;
    blocks.push(block);
    stage.addChild(block);
}

function createLevel() {
    var block = new createjs.Bitmap("Assets/Tiles/grassMid.png");
    block.snapToPixel = true;
    block.y = 4 * 70;
    blocks.push(block);
    console.log(block.getBounds());
    for (var i = 0; i < 10; i++) {
        var clone = block.clone();
        clone.x = i * 70;
        blocks.push(clone);
        stage.addChild(clone);
    }
}

function addItem() {
    iteme = new createjs.Bitmap("Assets/Items/bomb.png");
    stage.addChild(iteme);
}

function tick() {
    player.tick();
    stage.update();
}

function handleKeyDown(e) {
    player.jump();
    player.reset();
}

function handleKeyUp(e) {
    
}

function calculateIntersection(rect1, rect2, x, y) {
    x = x || 0;
    y = y || 0;

    var dx,
        dy,
        r1 = {
            cx: <number> null,
            cy: <number> null,
            hw: <number> null,
            hh: <number> null
        },
        r2 = {
            cx: <number> null,
            cy: <number> null,
            hw: <number> null,
            hh: <number> null
        };

    r1.cx = rect1.x + x + (r1.hw = (rect1.width / 2));
    r1.cy = rect1.y + y + (r1.hh = (rect1.height / 2));
    r2.cx = rect2.x + (r2.hw = (rect2.width / 2));
    r1.cy = rect1.y + (r1.hh = (rect2.height / 2));

    dx = Math.abs(r1.cx - r2.cx) - (r1.hw + r2.hw);
    dy = Math.abs(r1.cy - r2.cy) - (r1.hh + r2.hh);

    if (dx < 0 && dy < 0) {
        return { width: -dx, height: -dy };
    }
    else {
        return null;
    }
}

window.onload = () => {
    var canvas = document.getElementById("canvas");
    stage = new createjs.Stage("canvas");
    createjs.Ticker.setFPS(30);
    createjs.Ticker.addEventListener("tick", tick);
    //createLevel();
    //addItem();
    //console.log(player.getBounds());
    generateLevel(0);
    stage.addChild(player);
    drawHUD();
    stage.update();

    if ('ontouchstart' in document.documentElement) {
        canvas.addEventListener('touchstart', function (e) {
            handleKeyDown(e);
        }, false);
        canvas.addEventListener('touchend', function (e) {
            handleKeyUp(e);
        }, false);
    }
    else {
        document.onkeydown = handleKeyDown;
        document.onkeyup = handleKeyUp;
        document.onmousedown = handleKeyDown;
        document.onmouseup = handleKeyUp;
    }
};